package com.baidu.bulls.stock.admin.user.controller;

import com.baidu.bulls.stock.common.exception.BusinessException;
import com.baidu.bulls.stock.common.exception.constants.ApplicationErrorCodeEnum;
import com.baidu.bulls.stock.entity.user.AuthorityUser;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;

public class BaseController {
    //获得session对话
    protected HttpSession getSession(){
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpSession session = requestAttributes.getRequest().getSession();
        return session;
    }

    //获得当前登录用户
    public AuthorityUser getUser() throws BusinessException {
        HttpSession session = getSession();
        AuthorityUser attribute = (AuthorityUser) session.getAttribute(session.getId());
        if (attribute == null) {
            throw new BusinessException(ApplicationErrorCodeEnum.ADMIN_USER_NEED_LOGIN);
        }

        return attribute;

    }
}
